﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Draco.DataStructure;

namespace Draco.Algorithms
{
    /// <summary>
    /// 字符串相关算法
    /// </summary>
    public class D_String
    {
        /// <summary>
        /// 前缀匹配
        /// </summary>
        /// <param name="str">待匹配字符串</param>
        /// <param name="prefix">前缀</param>
        /// <returns>是否匹配</returns>
        public static bool PrefixMatching(string str, string prefix)
        {
            for (int i = 0; i < prefix.Length; i++)
            {
                if (str[i] != prefix[i])
                {
                    return false;
                }
            }
            return true;

        }


        /// <summary>
        /// 批量取子串
        /// </summary>
        public static D_LinkList<string> SubStringList(D_LinkList<string> strs, int startIndex,int len)
        {
            D_LinkList<string> sub = new D_LinkList<string>();
            foreach (string s in strs)
            {
                sub.Add(s.Substring(startIndex, len));
            }
            return sub;
        }


        /// <summary>
        /// 暴力搜索 Brute force
        /// </summary>
        /// <param name="str">待匹配字符串</param>
        /// <param name="sub">子串</param>
        /// <returns>是否匹配</returns>
        public static bool SubMatching(string str, string sub)
        {
            if (sub == "") return true;
            if (str == "" || sub.Length > str.Length) return false;

            for (int i = 0; i <= str.Length - sub.Length; i++)
            {
                for (int j = 0; j < sub.Length; j++)
                {
                    if (sub[j] == str[i+j])
                    {
                        if (j == sub.Length-1)
                        {
                            return true;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return false;
        }

    }
}
